<?php

namespace App\Http\Controllers\View;

use App\Entity\Category;
use App\Entity\PdtContent;
use App\Entity\PdtImages;
use App\Entity\Product;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;


class BookController extends Controller
{
    public function toCategory()
    {
        $categorys = Category::query()->whereNull('parent_id')->get();

        return view('category')->with('categorys',$categorys);
    }

    public function toProduct($category_id)
    {
        $products = Product::query()->where('category_id', $category_id)->get();
        return view('product')->with('products',$products);
    }

    public function toPdtContent($product_id,Request $request)
    {

        $product = Product::query()->find($product_id);
        $pdtImages = PdtImages::query()->where('product_id', $product_id)->get();
        $pdtContent = PdtContent::query()->where('product_id', $product_id)->first();





        $count = 0;

        $bk_cart = $request->cookie('bk_cart');
        $bk_cart_arr= ($bk_cart != null? explode(','  , $bk_cart) : array());


        foreach ($bk_cart_arr as $value) {
            $index = strpos($value, ':');
            if (substr($value,0,$index) == $product_id) {
                $count = substr($value,$index+1);
                break;
            }
        }


        return view('pdt_content')
            ->with('product',$product)
            ->with('pdt_images',$pdtImages)
            ->with('pdt_content',$pdtContent)
            ->with('count',$count);
    }
}
